#1.导包
import pandas as pd
import numpy as np
import xgboost as xgb
from collections import Counter

from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split,GridSearchCV, StratifiedKFold
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import classification_report, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score
from sklearn.utils import class_weight
#2.加载数据
data = pd.read_csv('D:/01_人才流失实战/人才流失预测/train.csv')
#3.了解数据
print(data.head())
print(data.info()) #TODO 一共31列数据 其中 标签为'Attrition'--离职与否

print(data.columns) #TODO 其中int类型的列一共23个，object类型的列有8个
print(data.value_counts('Attrition'))
print('=========================================================================================')
#4.数据预处理
#4.1处理缺失数据
print(data.isnull().sum())
# TODO 缺失数据为0
#4.2处理非数据形特征
# TODO 一共有8类特征为非数据类型 1.BusinessTravel 2.Department 3.EducationField 4.Gender 5.JobRole 6.MaritalStatus 7.Over18 8.OverTime
#处理1.BusinessTravel ['Travel_Rarely' 'Travel_Frequently' 'Non-Travel']
data.replace('Non-Travel',0,inplace=True)
data.replace('Travel_Rarely',1,inplace=True)
data.replace('Travel_Frequently',2,inplace=True)
#处理2.Department ['Research & Development' 'Sales' 'Human Resources']
data.replace('Human Resources',0,inplace=True)
data.replace('Research & Development',1,inplace=True)
data.replace('Sales',2,inplace=True)
#处理3.EducationField ['Life Sciences' 'Medical' 'Other' 'Technical Degree' 'Human Resources'  'Marketing']
data.replace('Life Sciences',0,inplace=True)
data.replace('Medical',1,inplace=True)
data.replace('Marketing',2,inplace=True)
data.replace('Technical Degree',3,inplace=True)
data.replace('Other',4,inplace=True)
#处理4.Gender ['Male' 'Female']
data.replace('Male',0,inplace=True)
data.replace('Female',1,inplace=True)
#处理5.JobRole ['Manufacturing Director' 'Laboratory Technician' 'Sales Executive' 'Research Scientist' 'Healthcare Representative' 'Human Resources' 'Sales Representative' 'Research Director' 'Manager']
data.replace('Manufacturing Director',0,inplace=True)
data.replace('Laboratory Technician',1,inplace=True)
data.replace('Sales Executive',2,inplace=True)
data.replace('Research Scientist',3,inplace=True)
data.replace('Healthcare Representative',4,inplace=True)
data.replace('Human Resources',5,inplace=True)
data.replace('Sales Representative',6,inplace=True)
data.replace('Research Director',7,inplace=True)
data.replace('Manager',8,inplace=True)
#处理6.MaritalStatus ['Divorced' 'Single' 'Married']
data.replace('Divorced',0,inplace=True)
data.replace('Single',1,inplace=True)
data.replace('Married',2,inplace=True)
#处理7.Over18 只有 Y 直接删除
data = data.drop(columns=['Over18'])
#处理8.OverTime ['No' 'Yes']
data.replace('No',0,inplace=True)
data.replace('Yes',1,inplace=True)

print('------------------------------------------------------------')
#5.特征工程
# TODO 注意！以下形式更加正规但是AUC值会下降！
'''
x_train=data.iloc[:,1:]
y_train=data.iloc[:,0]
'''
features=data.iloc[:,1:]
label=data.iloc[:,0]
x_train,x_test,y_train,y_test = train_test_split(features,label,test_size=0.2,random_state=35)
#6.创建模型
#选择逻辑回归模型

from sklearn.linear_model import LogisticRegression
model = LogisticRegression(max_iter=1000,class_weight='balanced')
# 定义参数空间
param_grid = {
    'C': [0.001, 0.01, 0.1, 1, 10, 100],  # 正则化强度
    'penalty': ['l1', 'l2'],  # 惩罚类型
    'solver': ['liblinear']  # 兼容 l1 和 l2 的求解器
}
#7.训练模型
# 创建 GridSearchCV 对象
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='accuracy')
grid_search.fit(x_train, y_train)
print('网格搜索结果==========================================')
print(f"最佳参数: {grid_search.best_params_}")
print(f"最佳得分(交叉验证): {grid_search.best_score_}")
'''网格搜索结果==========================================
最佳参数: {'C': 0.1, 'penalty': 'l1', 'solver': 'liblinear'}
最佳得分(交叉验证): 0.759090909090909'''


#8.处理测试集数据
test_data = pd.read_csv('D:/01_人才流失实战/人才流失预测/test2.csv')
#处理1.BusinessTravel ['Travel_Rarely' 'Travel_Frequently' 'Non-Travel']
test_data.replace('Non-Travel',0,inplace=True)
test_data.replace('Travel_Rarely',1,inplace=True)
test_data.replace('Travel_Frequently',2,inplace=True)
#处理2.Department ['Research & Development' 'Sales' 'Human Resources']
test_data.replace('Human Resources',0,inplace=True)
test_data.replace('Research & Development',1,inplace=True)
test_data.replace('Sales',2,inplace=True)
#处理3.EducationField ['Life Sciences' 'Medical' 'Other' 'Technical Degree' 'Human Resources'  'Marketing']
test_data.replace('Life Sciences',0,inplace=True)
test_data.replace('Medical',1,inplace=True)
test_data.replace('Marketing',2,inplace=True)
test_data.replace('Technical Degree',3,inplace=True)
test_data.replace('Other',4,inplace=True)
#处理4.Gender ['Male' 'Female']
test_data.replace('Male',0,inplace=True)
test_data.replace('Female',1,inplace=True)
#处理5.JobRole ['Manufacturing Director' 'Laboratory Technician' 'Sales Executive' 'Research Scientist' 'Healthcare Representative' 'Human Resources' 'Sales Representative' 'Research Director' 'Manager']
test_data.replace('Manufacturing Director',0,inplace=True)
test_data.replace('Laboratory Technician',1,inplace=True)
test_data.replace('Sales Executive',2,inplace=True)
test_data.replace('Research Scientist',3,inplace=True)
test_data.replace('Healthcare Representative',4,inplace=True)
test_data.replace('Human Resources',5,inplace=True)
test_data.replace('Sales Representative',6,inplace=True)
test_data.replace('Research Director',7,inplace=True)
test_data.replace('Manager',8,inplace=True)
#处理6.MaritalStatus ['Divorced' 'Single' 'Married']
test_data.replace('Divorced',0,inplace=True)
test_data.replace('Single',1,inplace=True)
test_data.replace('Married',2,inplace=True)
#处理7.Over18 只有 Y 直接删除
test_data=test_data.drop(columns=['Over18'])
#处理8.OverTime ['No' 'Yes']
test_data.replace('No',0,inplace=True)
test_data.replace('Yes',1,inplace=True)


#分割数据集和测试集合
x_test,y_test = test_data.iloc[:,:-1],test_data.iloc[:,-1]


#9.使用最佳模型进行预测
best_model = grid_search.best_estimator_
y_predict = best_model.predict(x_test)
#10.模型评估
print('直接修改==========================================')
print(f"准确率(accuracy):{accuracy_score(y_test, y_predict)}")
print(f"精确率(precision):{precision_score(y_test, y_predict, pos_label=0)}")
print(f"召回率(recall):{recall_score(y_test, y_predict, pos_label=0)}")
print(f"f1分数:{f1_score(y_test, y_predict, pos_label=0)}")
print(f"roc_auc_score:{roc_auc_score(y_test, y_predict)}")
print(f"分类评估报告:{classification_report(y_test, y_predict)}")
#11.数据可视化
print('======================================================================')
# 计算相关性矩阵
corr_matrix = data.corr()

# 绘制热力图
import seaborn as sns
plt.figure(figsize=(22, 20))
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f')
plt.title('Correlation Matrix')
plt.show()
# TODO 我啥也看不出来